/***************************************************************************
 * Copyright 2012 TXT e-solutions SpA
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the research area ICT-2009.1.3
 * Internet of Things and enterprise environments.
 *
 * Authors:
 *      Salvatore Piccione (TXT e-solutions SpA)
 *
 * Contributors:
 *        Domenico Rotondi (TXT e-solutions SpA)
 **************************************************************************/
package it.txt.access.capability.revocation.wizard.util;

import it.txt.access.capability.commons.schema.validation.CapabilitySchemaValidationHandler;
import it.txt.access.capability.commons.schema.validation.CapabilitySchemaValidationHandlerException;
import it.txt.access.capability.commons.utils.XMLPrinter;
import it.txt.access.capability.factory.CapabilitySchemaFactory;
import it.txt.access.capability.factory.CapabilitySchemaFactoryException;
import it.txt.access.capability.schema.AccessRightsCapabilityType;
import it.txt.access.capability.verifier.CapabilityVerifier;
import it.txt.access.capability.verifier.CapabilityVerifierException;
import it.txt.access.capability.verifier.CapabilityVerifierValues;
import it.txt.access.capability.verifier.VerifiedCapability;

import java.awt.Component;
import java.awt.Cursor;
import java.io.File;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

import org.w3c.dom.Document;

/**
 * @author Salvatore Piccione (TXT e-solutions SpA - salvatore.piccione AT network.txtgroup.com)
 *
 */
public class AuCapTokenLoader {
    
    private static final Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
    private static final Cursor busyCursor = new Cursor(Cursor.WAIT_CURSOR);
    private static final Logger LOGGER = Logger.getLogger(AuCapTokenLoader.class.getName());
        
    public static AccessRightsCapabilityType load (File file, Component wizard) {
        AccessRightsCapabilityType capability = null;
        wizard.setCursor(busyCursor);

        String title = "Loading capability token";

        String message = "Successful load of the capability token!";
        
        boolean isErrorMessage = false;

        try {
            capability = 
                    CapabilitySchemaFactory.getInstance().createAccessRights(file);

            CapabilityVerifierValues values = new CapabilityVerifierValues();

            ArrayList<VerifiedCapability> vcs = new ArrayList<VerifiedCapability>();

            if (CapabilityVerifier.accessRightsCapabilityValidityCheck(capability, values, vcs)) {

                LOGGER.log(Level.INFO, "Capability Chain:");
                LOGGER.log(Level.INFO, "************************");
                for (VerifiedCapability verifiedCapability : vcs) {
                    LOGGER.log(Level.INFO, "Capability ID: {0}", verifiedCapability.getCapabilityID());
                    LOGGER.log(Level.INFO, "Capability Issue Date: {0}", verifiedCapability.getIssueDate().toString());
                    LOGGER.log(Level.INFO, "************************");
                }
                Document capabilityDoc = CapabilitySchemaFactory.getInstance().createAccessRightsDocument(capability);
                XMLPrinter.logDocumentElement(capabilityDoc.getDocumentElement(), "Imported Capability", false);
            } else {
                message = "The Capability Validity verifier returned false!";
            }
        }
        catch (CapabilitySchemaFactoryException ex) {
            isErrorMessage = true;
            message = ex.getMessage();
        } catch (CapabilityVerifierException ex) {
            isErrorMessage = true;
            message = ex.getMessage();
        } catch (CapabilitySchemaValidationHandlerException ex) {
            isErrorMessage = true;
            CapabilitySchemaValidationHandler.dumpValidationEvents(
            ex.getValidationEvents());
            message = ex.getMessage();
        }

        int type = isErrorMessage ? JOptionPane.ERROR_MESSAGE : JOptionPane.INFORMATION_MESSAGE;
        JOptionPane.showMessageDialog(wizard, message, title, type );

        wizard.setCursor(defaultCursor);

        return capability;
    }

}
